/* window.c
 * This file is part of the Tisa Project.
 *  
 * Authors: Cleiber Marques da Silva <cleiber.dev@gmail.com>
 *          Odilson Tadeu Valle <odilson@sj.cefetsc.edu.br>
 *
 * Tisa Project is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; version 2 of
 * the License.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <curses.h>

#include "tisa.h"

#define ENTER 10
#define ESC 27

static const char *get_variable_string(int variable)
{
	switch (variable) {
	case Ta:
		return "Ta";
	case T:
		return "T";
	case Ti:
		return "Ti";
	case No:
		return "No";
	case H:
		return "H";
	case Ni:
		return "Ni";
	case Q:
		return "Q";
	default:
		return "";
	}
}

static void create_menubar(WINDOW *menubar)
{
	wbkgd(menubar, COLOR_PAIR(2));
	wattron(menubar, COLOR_PAIR(3)); 
	waddstr(menubar, "Tisa 0.1");
	wattroff(menubar, COLOR_PAIR(3)); 
}

/* XXX This is very UGLY XXX */
void window_init(struct boiler *heater)
{
	int i, j;
	WINDOW *menu;

	/* Init ncurses */
    initscr();
	start_color();
	
	/* Define colors */
	init_pair(1, COLOR_GREEN, COLOR_BLACK); 
    init_pair(2, COLOR_BLACK, COLOR_GREEN); 
    init_pair(3, COLOR_RED, COLOR_GREEN);
	init_pair(4, COLOR_GREEN, COLOR_RED);

	curs_set(0);
	noecho();
    keypad(stdscr, TRUE);
	bkgd(COLOR_PAIR(1));
	menu = subwin(stdscr, 1, 80, 0, 0);
	create_menubar(menu);

    attron(COLOR_PAIR(2)); 
    mvprintw(21, 12, "F5");
	mvprintw(21, 42, "F6");
    attroff(COLOR_PAIR(2));

	mvprintw(21, 15, "TRef");
	mvprintw(21, 45, "HRef");
	refresh();

	/* Create windows to variables */
	for (i = 0, j = 5; i < 4; i++) {
		mvprintw(3, j, get_variable_string(i));
		j += 18;
		refresh();
		heater->win[i] = newwin(2, 10, 3, (i * 10) + (8 * (i + 1)));
		wbkgd(heater->win[i], COLOR_PAIR(3));
		//box(heater->win[i], ACS_VLINE, ACS_HLINE);
		wrefresh(heater->win[i]);
	}

	for (i = 0, j = 9; i < 3; i++) {
		mvprintw(8, j, get_variable_string(i + 4));
		j += 22;
		refresh();
		heater->win[i + 4] = newwin(2, 10, 8, (i * 10) + (12 * (i + 1)));
		wbkgd(heater->win[i + 4], COLOR_PAIR(3));
		//box(heater->win[i + 4], ACS_VLINE, ACS_HLINE);
		wrefresh(heater->win[i + 4]);
	}

	heater->win[ALARMS] = newwin(5, 40, 13, 20);
	wbkgd(heater->win[ALARMS], COLOR_PAIR(4));
	box(heater->win[ALARMS], ACS_VLINE, ACS_HLINE);
	wattron(heater->win[ALARMS], COLOR_PAIR(3)); 
    mvwprintw(heater->win[ALARMS], 1, 17, "Alarms");
    wattroff(heater->win[ALARMS], COLOR_PAIR(3));
	wrefresh(heater->win[ALARMS]);

	heater->win[TREF] = newwin(1, 10, 21, 20);
	wbkgd(heater->win[TREF], COLOR_PAIR(3));
//	box(heater->win[TREF], ACS_VLINE, ACS_HLINE);
	wrefresh(heater->win[TREF]);

	heater->win[HREF] = newwin(1, 10, 21, 50);
	wbkgd(heater->win[HREF], COLOR_PAIR(3));
//	box(heater->win[HREF], ACS_VLINE, ACS_HLINE);
	wrefresh(heater->win[HREF]);
}

void window(struct boiler *heater)
{
	int key;
	char str[10];

	do {
		key = getch();

		if (key == KEY_F(5)) {
			echo();
			wgetstr(heater->win[TREF], str);
		} else if (key == KEY_F(6)) {
			echo();
			wgetstr(heater->win[HREF], str);
		}
		noecho();
	} while (key != ESC);
}

void window_close(struct boiler *heater)
{
	endwin();
}
